package main

import (
	"flag"
	"fmt"
	"github.com/gin-gonic/gin"
	"oa-rest/common"
	"oa-rest/config"
	"oa-rest/middleware"
	"oa-rest/models/user"
	"xorm.io/xorm"
)

var db *xorm.Engine
var err error

var serverPort = flag.String("port", "8080", "server port")

func main() {
	flag.Parse()
	config.InitEnv()
	db, err = common.ConnectDb()
	if err != nil {
		panic("初始化数据库失败：" + err.Error())
	}
	r := gin.New()
	r.Use(middleware.RequestId())
	r.Use(middleware.OpLog())
	r.Use(middleware.Cors())

	r.Use(gin.Recovery())
	apiV1 := r.Group("/api")
	apiV1.GET("/user/captcha/:captchaId", func(c *gin.Context) {
		captchaId := c.Param("captchaId")
		fmt.Println("GetCaptchaPng : " + captchaId)
		common.ServeHTTP(c.Writer, c.Request)
	})
	if config.JwtEnabled {
		apiV1.Use(middleware.JWT())
	}

	userImp := user.NewUser(db)
	userImp.InitApi(apiV1)
	r.Run(":" + *serverPort)
}
